#!/usr/bin/perl -w
use strict;
use protect;

chdir '/usr/Sky/down/queue';

my $fake=0;

my $ord=0;
if ($fake || !-t STDOUT)
{
use Fcntl ':flock';

open STDOUT,'>> ../fetch.log';
open STDERR,'>> ../fetch.log';
flock STDOUT,LOCK_EX;
for ($ord=2;$ord>0;$ord--)
{
flock(STDOUT,LOCK_EX),last unless fork;
}
}

$\="\n";$,="\t";

use DBI;

our $h=DBI->connect('DBI:mysql:database=Down','root') || die $DBI::errstr;

$h->do("truncate table locked") unless $ord;
flock(STDOUT,LOCK_UN);

#my $lock1=$h->prepare('lock tables locked write');
#my $lock0=$h->prepare('unlock tables');
my $lockput=$h->prepare('insert into locked values (sha1(?),?,?)');
my $lockget=$h->prepare('select id,pid from locked where urlhash=sha1(?)');
my $lockout=$h->prepare(qq/update down set status=? where id=? and status=0/);

our $ns=$h->prepare(qq/select id,status,url,filename,post from down where status in (0) order by prio desc,(rand()+0.5)*(size+1)/);
our $done=$h->prepare(qq/update down set nowsize=?,filename=?,`log`=?,finished=now(),status=? where id=?/);

my ($id,$status,$url,$sz,$fn,$post,$a);

$ns->execute();

$ENV{'TZ'}='EET'; # we should live as our sattelitte provider

while (($id,$status,$url,$fn,$post)=$ns->fetchrow_array())
{
#next if (($id & 1) == $ord);
next unless $url =~ /^(f|ht)tps?:/;
next if $status;

#$lock1->execute() or die $DBI::errstr;
flock(STDOUT,LOCK_EX);
$a=$lockput->execute($url,$id,$$);

unless ($a)
{

#$lockget->execute($url);
#my ($iid,$pid)=$lockget->fetchrow_array();

#if ($iid && ($iid != $id))
#{
#print(STDERR scalar(localtime),$$,$pid,"DUPE",$id,$iid);
#$lockout->execute(3,$id);
#}

}

flock(STDOUT,LOCK_UN);
#$lock0->execute() or die $DBI::errstr;
next unless $a;

# cheap time - only from 2:00 till 6:00 EET
my $hour=(localtime())[2];
print(STDERR "Cheap time exceeded!\n"),exit if !$fake && $hour>=6; # do not overpay

$url=~s{ftp\.efind}{ftpr.efind}g; # sorry - no risk

protect::DO(\$url),#=~s{([^\w.\-_=\?\&:/@!%])}{'%'.unpack('H2',$1)}eg;
$a='';

protect::DO(\$post),#=~s{([^\w.\-_=\?\&:/@!%])}{'%'.unpack('H2',$1)}eg,
$a.="--post-data='$post' "
if defined($post) && $post ne '';

protect::DO(\$fn),#=~s{([^\w.\-_=\?\&:/@!%])}{'%'.unpack('H2',$1)}eg,
$a.="--output-document='$fn' "
if defined($fn) && $fn ne '';

{
#open LOG,'>>../fetch.log';
print STDERR scalar(localtime),$$,$url,$a;
#close LOG;
}

#die;

if ($fake)
{
sleep(2);
#print "$$:Get '$url' $a\n";
#sleep(2);
} else {

$_=`wget -t 5 -dc '$url' $a --proxy=off --progress=dot:binary 2>&1`;
#print "$_";
#$sz=0;$sz=$1 if /Content-Length: (\d+)/i;
#$hdr->execute($sz,$_,$id);
$fn=$1 if /\n +=> \`(.*?)\'/; #`

$done->execute(-s $fn, $fn, $_, 1,$id);

my $path='../done/';
$path.='ubuntu/' if $fn=~ /\.u?deb$/;
$path.='upd/inc/' if $fn=~ /\.(upd|gem)$/i;
rename $fn,$path.$fn;
}

}

__END__

#our $s=$h->prepare(qq/INSERT ignore $table ($pv) values ($pq)/);

no utf8;
open IN,"don";
open STDERR,'>/dev/null';
while (my $url=<IN>)
{
chomp $url;
#$a=$_;
#print $_,"\n" unless $a eq $_;
$_=`HEAD '$url' -p http://lo:3128/`;
#http://mine:9202/;
my $sz=0;
$sz=$1 if /Content-Length: (\d+)/i;
if ($sz){printf "%5.1f %s\n",$sz/1024/1024,$url}else{print}
}

#!/bin/bash
cd /opt/Sat/in
wget -bci ../don --proxy=off
